// pages/seekrent/seekrent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
		markers: [{
      id: 1,
      latitude: 23.099994,
      longitude: 113.324520,
      name: 'T.I.T 创意园'
    }],
    covers: [{
      latitude: 23.099994,
      longitude: 113.344520,
      iconPath: '/image/TF6_~X1{X$AHZS0(7Q$~))9.png'
    }, {
      latitude: 23.099994,
      longitude: 113.304520,
      iconPath: '/image/TF6_~X1{X$AHZS0(7Q$~))9.png'
    }],
      list:[],
      id:"",
      latitude:"",
      longitude:"",
      address:"",
      addressid:"",
      addresslist:[],
      house_type: [],
      province:[],
      province_id:[],
      type:"",
      region:"",
      type_id:[],
			type_ids:"",
			show:true,
			count:0,
			text:"点击查看位置"
  },
	showaddress(){
		var count = this.data.count
		count++
		if(count%2!=0){
			this.setData({
			show:false,
			count:count,
			text:"点击关闭位置"
		})
		}else{
			this.setData({
				show:true,
				count:count,
				text:"点击查看位置"
			})
		}
		
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var _this=this;
    this.setData({
      id:options.id
    })
    wx.getLocation({
      type: 'wgs84',
      success (res) {
				_this.setData({
					latitude:res.latitude,
					longitude:res.longitude
				})
       wx.request({
         url: 'http://www.house.com/index.php/house/address',
         method:"POST",
         data:{latitude:res.latitude,longitude:res.longitude},
         success:res=>{
          if(res.data.code==200){
            _this.setData({
              address:res.data.data.address,
							addressid:res.data.data.pid
            })
            _this.search();
            _this.getAddress();
          }
         }
       })
      }
     })
      wx.request({
        url: 'http://www.house.com/index.php/house/province',
        success:res=>{
          if(res.data.code==200){
            var _this=this;
              res.data.data.map(function(e){
                let province = _this.data.province
                let province_id = _this.data.province_id
                let new_house_type = province.concat(e.name)
                let new_province_id= province_id.concat(e.id);
                _this.setData({
                  province:new_house_type,
                  province_id:new_province_id
                })
              })
          }
        }
      })
      wx.request({
        url: 'http://www.house.com/index.php/house/type',
        success:res=>{
          if(res.data.code==200){
            var _this=this;
              res.data.data.map(function(e){
                let house_type = _this.data.house_type
                let type_id = _this.data.type_id
                let new_house_type = house_type.concat(e.house_type_name)
                let new_type_id= type_id.concat(e.id)
                _this.setData({
                  house_type:new_house_type,
                  type_id:new_type_id
                })
              })
          }
        }
      })
  },
  getAddress(){
    wx.request({
      url: 'http://www.house.com/index.php/house/region?id='+this.data.addressid,
      success:res=>{
        if(res.data.code==200){
          var _this=this;
          _this.data.addresslist=[]
            res.data.data.map(function(e){
              let addresslist = _this.data.addresslist
              let new_house_type = addresslist.concat(e.name)
              _this.setData({
                addresslist:new_house_type
              })
            })
        }
      }
    })
  },
  bindPickerProvince(e){
    this.setData({
      address:this.data.province[e.detail.value],
      addressid:this.data.province_id[e.detail.value],
      type:"",
      region:""
    })
    this.getAddress()
  },
  bindPickerRegion(e){
    this.setData({
      region:this.data.addresslist[e.detail.value]
    })
  },
  bindPickerChange(e){
    this.setData({
      type:this.data.house_type[e.detail.value],
      type_ids:this.data.type_id[e.detail.value]
    })
  },
  search(){
    var address=this.data.address
    var type=this.data.type_ids
    var region = this.data.region
    wx.request({
      url: 'http://www.house.com/index.php/house/list?id='+this.data.id,
      data:{
        address:address,
        type:type,
        region:region
      },
      success:res=>{
        if(res.data.code==200){
          this.setData({
            list:res.data.data
          })
        }else{
          wx.showToast({
            title: '无数据',
          })
          this.setData({
            list:res.data.data
          })
        }
      }
    })
  },
  phone(e){
    console.log(e.currentTarget.dataset.uid)
    wx.request({
      url: 'http://www.house.com/index.php/house/phone?uid='+e.currentTarget.dataset.uid,
      success:res=>{
       wx.makePhoneCall({
         phoneNumber: res.data.data.user_phone,
       })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})